// packageIndex/pages/problem/problem.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      title:'',
      content:'',
      time:'',
      inputValue: '',
      maxLength: 30, // 初始设置最大长度为30个字节
      exceededLength: false
  },

  onBottom(event){
      this.setData({
        title:event.detail.value
      })
      console.log(this.data.title);
  },

   addData(){
    wx.request({
      url: 'http://localhost:8081/sfzy/problems',
      method:'POST',
      data:{
        id:'',
        problemTitle:this.data.title,
        problemContent:this.data.content,
        problemUserId:'3',
        problemReleaseTime:this.data.time,
        problemCategory:'',
        del:''
      },
      success:(res)=>{
        console.log(res.data.data);

      }
    })
   },
   getCurrentDate() {
    const now = new Date();
    const year = now.getFullYear(); // 获取年份
    const month = now.getMonth() + 1; // 获取月份，月份需要+1因为getMonth()返回的月份从0开始
    const day = now.getDate(); // 获取日
    const d = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
    //获取 时分
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const t =`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
    this.setData({
      
      time:d+" "+t
    })
  },
  handleInput(e) {
     this.setData({
      content:e.detail.value
    })
    
    const value = e.detail.value;
    const byteLength = this.getByteLength(value);

    if (byteLength > this.maxLength) {
      this.setData({
        inputValue: value.slice(0, this.findMaxByteLengthIndex(value, this.maxLength)),
        exceededLength: true
      });
    } else {
      this.setData({
        inputValue: value,
        exceededLength: false
      });
    }
  },
  getByteLength(str) {
    let len = 0;
    for (let i = 0; i < str.length; i++) {
      const code = str.charCodeAt(i);
      if (code <= 0x007F) {
        len += 1; // ASCII
      } else if (code <= 0x07FF) {
        len += 2; // 非ASCII字符，可能是两个字节
      } else if (code <= 0xFFFF) {
        len += 3; // 这里通常表示UTF-16字符，但UTF-8中可能是3个字节
      } else if (code <= 0x10FFFF) {
        len += 4; // 通常是四个字节的UTF-8字符
      }
    }
    return len;
  },
  findMaxByteLengthIndex(str, maxBytes) {
    let byteCount = 0;
    let maxIndex = 0;

    for (let i = 0; i < str.length; i++) {
      const code = str.charCodeAt(i);
      if (code <= 0x007F) {
        byteCount += 1;
      } else if (code <= 0x07FF) {
        byteCount += 2;
      } else if (code <= 0xFFFF) {
        byteCount += 3; // 但在UTF-16中，仍然算作2个字节
      } else if (code <= 0x10FFFF) {
        byteCount += 4;
      }

      if (byteCount > maxBytes) {
        break;
      }
      maxIndex = i;
    }
    return maxIndex;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})